package com.ewzj.bigdata.mvp.presenter

import android.app.Application
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.CitySelectorActivityContract
import com.ewzj.bigdata.mvp.contract.ScanRecordActivityContract
import com.ewzj.bigdata.mvp.model.bean.CitygroupBean
import com.ewzj.bigdata.mvp.model.reqbean.*
import com.ewzj.bigdata.mvp.model.rspbean.BaseData
import com.ewzj.bigdata.mvp.model.rspbean.CityBean
import com.ewzj.bigdata.mvp.model.rspbean.CitySearchBean
import com.ewzj.bigdata.mvp.model.rspbean.CollectionBean
import com.ewzj.bigdata.mvp.ui.adapter.CityAdapter
import com.ewzj.bigdata.mvp.ui.adapter.CollectionAdapter
import com.ewzj.bigdata.mvp.ui.adapter.HotcityAdapter
import com.google.gson.Gson
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.utils.DataHelper
import com.jess.arms.utils.UiUtils
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observables.GroupedObservable
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import timber.log.Timber
import java.util.*
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class ScanRecordActivityPresenter @Inject constructor(val mGson: Gson, val mErrorHandler: RxErrorHandler, val application: Application, view: ScanRecordActivityContract.View, model: ScanRecordActivityContract.Model) : BasePresenter<ScanRecordActivityContract.Model, ScanRecordActivityContract.View>(model, view) {
    val datas = arrayListOf<CollectionBean>()
    val adapter = CollectionAdapter(datas,1)
    var page=1
    val context=mRootView as AppCompatActivity
    fun loadScanRecord() {
        page=1
        mModel.loadRecentScan(ReqsearchBean()).compose(RxUtils.applySchedulers(mRootView))
                .subscribe({ t ->
                    page++
                    datas.clear()
                    datas.addAll(t.data!!.list)
                    adapter.setNewData(datas)
                    if (t.data!!.list.size<AppKey.PAGE_NUM){
                        adapter.disableLoadMoreIfNotFullPage()
                    }
                })
    }
    fun loadMoreData(){
        mModel.loadRecentScan(ReqsearchBean(page)).compose(RxUtils.bindToLifecycle(mRootView))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doAfterTerminate { if (adapter.isLoading){
                    adapter.loadMoreComplete()
                } }
                .subscribe({t -> run{
                    page++
                    adapter.addData(t.data!!.list)
                    if (t.data!!.list.size<AppKey.PAGE_NUM){
                        adapter.loadMoreEnd()
                    }
                } },{adapter.loadMoreFail()})
    }
    fun initAdapter(recyclerView: RecyclerView){
        recyclerView.layoutManager=LinearLayoutManager(context)
        recyclerView.addItemDecoration(HorizontalDividerItemDecoration.Builder(context).colorResId(R.color.color_divider)
                .size(2).margin(5, 5).build())
        adapter.setOnLoadMoreListener(object :BaseQuickAdapter.RequestLoadMoreListener{
            override fun onLoadMoreRequested() {
                loadMoreData()
            }

        },recyclerView)
        adapter.cancelListener=object :CollectionAdapter.CancelListener{
            override fun cancelsub(collectionBean: CollectionBean) {
                datas.remove(collectionBean)
                adapter.notifyDataSetChanged()
                Observable.just(collectionBean).map { t -> val reqNormalCollect = ReqNormalCollect()
                run{

                    reqNormalCollect.list_id=collectionBean.getId().toString()
                    reqNormalCollect.table_id=collectionBean.table_id.toString()
                }
                    reqNormalCollect}.flatMap { t ->mModel.delRecentScan(t)  }
                        .compose(RxUtils.applySchedulers(mRootView))
                        .subscribe(object :ErrorHandleSubscriber<BaseData<Any>>(mErrorHandler){
                            override fun onNext(t: BaseData<Any>?) {
                                Timber.d("删除成功")
                            }

                        })
            }

        }
        recyclerView.adapter=adapter
    }

}